import tornado.web
import tornado.ioloop
import tornado.httpserver
import tornado.options
import os
from concurrent.futures import ThreadPoolExecutor
from tornado.concurrent import run_on_executor

from spider.dyld.dyld import dyld
from spider.dyld.dyldsearch import dyldsearch
from spider.dyld.dylddetail import dylddetail
from spider.dyld.dyldplay import dyldplay
from spider.dyld.dyldclassfy import dyldclassfy

from spider.cxy.cxysearch import cxysearch

from spider.douban.douban import douban

import spider.tv.tencent
import spider.tv.aiqiyi

import spider.movie.tencent
import spider.movie.aiqiyi

import spider.variety.tencent
import spider.variety.aiqiyi

from tornado.options import options
from tornado.web import RequestHandler, StaticFileHandler

tornado.options.define("port", type=int, default=9000, help="服务器端口")


class IndexHandler(RequestHandler):

    # 主页处理类
    def get(self):
        self.render("index.html")

    def post(self):
        self.render("index.html")


class SearchHandler(RequestHandler):

    # 主页处理类
    def get(self):
        self.render("search.html")

    def post(self):
        self.render("search.html")


class OutHandler(RequestHandler):

    # 主页处理类
    def get(self):
        self.render("out.html")

    def post(self):
        self.render("out.html")


class DetailHandler(RequestHandler):

    # 主页处理类
    def get(self):
        self.render("detail.html")

    def post(self):
        self.render("detail.html")


class PlayHandler(RequestHandler):

    # 主页处理类
    def get(self):
        self.render("play.html")

    def post(self):
        self.render("play.html")


class ClassifyHandler(RequestHandler):

    # 主页处理类
    def get(self):
        self.render("classify.html")

    def post(self):
        self.render("classify.html")


class DoubanHandler(RequestHandler):

    # 主页处理类
    def get(self):
        self.render("douban.html")

    def post(self):
        self.render("douban.html")


class ToolHandler(RequestHandler):

    # 主页处理类
    def get(self):
        self.render("tool.html")

    def post(self):
        self.render("tool.html")


class DyldHomeHandler(RequestHandler):
    executor = ThreadPoolExecutor(10)

    @run_on_executor()
    def post(self):
        line = self.get_argument("line")
        result = dyld(self, line)
        data = {
            "data": result,
        }
        self.write(data)


class DyldSearchHandler(RequestHandler):
    executor = ThreadPoolExecutor(10)

    @run_on_executor()
    def post(self):
        line = self.get_argument("line")
        page = self.get_argument("page")
        searchword = self.get_argument("searchword")
        resultDYLD = dyldsearch(self, line, page, searchword)
        data = {
            "data": resultDYLD,
        }
        self.write(data)


class CxySearchHandler(RequestHandler):
    executor = ThreadPoolExecutor(10)

    @run_on_executor()
    def post(self):
        outline = self.get_argument("outline")
        keyword = self.get_argument("searchword")
        page = self.get_argument("page")
        sort = self.get_argument("sort")
        page_size = self.get_argument("pageSize")
        result = cxysearch(self, outline, keyword, page, sort, page_size)

        data = {
            "data": result,
        }
        self.write(data)


class DyldDetailHandler(RequestHandler):
    executor = ThreadPoolExecutor(10)

    @run_on_executor()
    def post(self):
        line = self.get_argument("line")
        link = self.get_argument("link")
        result = dylddetail(self, line, link)
        data = {
            "data": result,
        }
        self.write(data)


class DyldPlayHandler(RequestHandler):
    executor = ThreadPoolExecutor(10)

    @run_on_executor()
    def post(self):
        line = self.get_argument("line")
        link = self.get_argument("link")
        result = dyldplay(self, line, link)
        data = {
            "data": result,
        }
        self.write(data)


class DoubanHomeHandler(RequestHandler):
    executor = ThreadPoolExecutor(10)

    @run_on_executor()
    def post(self):
        type = self.get_argument("type")
        page_limit = self.get_argument("page_limit")
        page_start = self.get_argument("page_start")
        tag = self.get_argument("tag")
        sort = self.get_argument("sort")
        result = douban(self, type, page_limit, page_start, tag, sort)
        data = {
            "data": result,
        }
        self.write(data)


class DyldClassifyHandler(RequestHandler):
    executor = ThreadPoolExecutor(10)

    @run_on_executor()
    def post(self):
        line = self.get_argument("line")
        search = self.get_argument("search")
        result = dyldclassfy(self, line, search)
        data = {
            "data": result,
        }
        self.write(data)


class TvpalyHandler(RequestHandler):
    executor = ThreadPoolExecutor(10)

    @run_on_executor()
    def post(self):
        name = self.get_argument("name")
        type = self.get_argument("type")
        if type == '1':
            resultTencent = spider.movie.tencent.tencents(self, name)
            resultAiqiyi = spider.movie.aiqiyi.aiqiyi(self, name)
            data = {
                "tencent": resultTencent,
                "aiqiyi": resultAiqiyi,
            }
            self.write(data)
        elif type == '2':
            resultTencent = spider.tv.tencent.tencents(self, name)
            resultAiqiyi = spider.tv.aiqiyi.aiqiyi(self, name)
            data = {
                "tencent": resultTencent,
                "aiqiyi": resultAiqiyi,
            }
            self.write(data)
        elif type == '3':
            resultTencent = spider.variety.tencent.tencents(self, name)
            resultAiqiyi = spider.variety.aiqiyi.aiqiyi(self, name)
            data = {
                "tencent": resultTencent,
                "aiqiyi": resultAiqiyi,
            }
            self.write(data)


if __name__ == '__main__':
    tornado.options.parse_command_line()
    current_path = os.path.dirname(__file__)
    app = tornado.web.Application(
        [
            (r'/', IndexHandler),
            (r'/search', SearchHandler),
            (r'/out', OutHandler),
            (r'/detail', DetailHandler),
            (r'/play', PlayHandler),
            (r'/douban', DoubanHandler),
            (r'/tool', ToolHandler),
            (r'/classify', ClassifyHandler),
            (r'/douban/home', DoubanHomeHandler),
            (r'/dyld/home', DyldHomeHandler),
            (r'/dyld/search', DyldSearchHandler),
            (r'/cxy/search', CxySearchHandler),
            (r'/dyld/detail', DyldDetailHandler),
            (r'/dyld/play', DyldPlayHandler),
            (r'/dyld/classify', DyldClassifyHandler),
            (r'/tool/tvplay', TvpalyHandler),
            (r'/(.*)', StaticFileHandler, {
                "path": os.path.join(current_path, "statics/html"),
                "default_filename": "index.html"
            }),
        ],
        static_path=os.path.join(current_path, "static"),
        template_path=os.path.join(current_path, "template"),
        debug=True,
        autoescape=None
    )

    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.current().start()
